package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.api.content.model.CourseTeacherDTO;
import com.xuecheng.common.enums.common.CommonEnum;
import com.xuecheng.common.enums.content.CourseAuditEnum;
import com.xuecheng.common.exception.ExceptionCast;
import com.xuecheng.content.common.constant.ContentErrorCode;
import com.xuecheng.content.convert.CourseTeacherConvert;
import com.xuecheng.content.entity.CourseBase;
import com.xuecheng.content.entity.CourseTeacher;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.CourseTeacherService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 课程-教师关系表 服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class CourseTeacherServiceImpl extends ServiceImpl<CourseTeacherMapper, CourseTeacher> implements CourseTeacherService {

    @Autowired
    private CourseBaseService courseBase;

    /**
     * @param courseBaseId
     * @return com.xuecheng.api.content.model.TeachplanDTO
     * @Description //獲取教師信息
     * @Date 2021/12/15 20:46
     * @Param [courseBaseId]  課程信息id
     * 業務分許
     * 事務不開
     * 關鍵數據-->課程id必須要有-->判斷課程是否被刪除.狀態是否為未提交或者審核未通過.是否被邏輯刪除
     * 業務數據
     */
    @Override
    public List<CourseTeacherDTO> queryTeacher(Long courseBaseId, Long companyId) {
        //判斷關鍵數據
        if (ObjectUtils.isEmpty(courseBaseId) || ObjectUtils.isEmpty(companyId)) {
            ExceptionCast.cast(ContentErrorCode.E_120013);
        }
        LambdaQueryWrapper<CourseBase> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CourseBase::getId, courseBaseId);
        wrapper.eq(CourseBase::getStatus, CommonEnum.USING_FLAG.getCodeInt());
        wrapper.eq(CourseBase::getCompanyId, companyId);
        CourseBase courseBaseOne = this.courseBase.getOne(wrapper);
        if (ObjectUtils.isEmpty(courseBaseOne)) {
            ExceptionCast.cast(ContentErrorCode.E_120013);
        }
        if (!(CourseAuditEnum.AUDIT_UNPAST_STATUS.getCode().equals(courseBaseOne.getAuditStatus())) &&
                !(CourseAuditEnum.AUDIT_DISPAST_STATUS.getCode().equals(courseBaseOne.getAuditStatus()))
        ) {
            ExceptionCast.cast(ContentErrorCode.E_1200504);
        }

        //構建查詢對象
//        LambdaQueryWrapper<CourseBase> queryWrapper = new LambdaQueryWrapper<>();
        Page<CourseTeacher> teachplanPage = new Page<>();


        LambdaQueryWrapper<CourseTeacher> queryWrap = new LambdaQueryWrapper<>();
        queryWrap.eq(CourseTeacher::getCourseId, courseBaseId);
        Page<CourseTeacher> page = this.page(teachplanPage, queryWrap);

        long total = page.getTotal();

        List<CourseTeacher> records = page.getRecords();
        List<CourseTeacherDTO> teacherDTOS = CourseTeacherConvert.INSTANCE.pos2Dtos(records);
//        PageVO<CourseTeacher> courseTeachers = new PageVO<>();
//        courseTeachers.setItems(records);
//        courseTeachers.setCounts(total);


//        CourseTeacher courseTeacher = this.list(queryWrapper).get(0);
//
//
//
//
//
//
//
//
//
//
//        CourseTeacherDTO courseTeacherDTO = CourseTeacherConvert.INSTANCE.po2dto(courseTeacher);
//
        return teacherDTOS;
    }

    /**
     * @param courseTeacherDTO
     * @param companyId
     * @return com.xuecheng.api.content.model.CourseTeacherDTO
     * @Description //課程新增或修改
     * @Date 2021/12/15 22:22
     * @Param [courseTeacherDTO, companyId] 保存信息和機構id
     * 業務分析
     * 開啓事務
     * 關鍵數據判斷-->courseId phot posi teach
     * 業務分析-->教師機構是否正確.是否被刪除.試否是未提叫或是審核未通過.
     * 判斷是修改還是新增-->有無教師id
     * 開始修改或者新增
     */
    @Override
    @Transactional
    public CourseTeacherDTO insertAndUpdateTeacherDTO(CourseTeacherDTO courseTeacherDTO, Long companyId) {
        //判斷關鍵數據
        if (ObjectUtils.isEmpty(courseTeacherDTO.getCourseId()) ||
                StringUtils.isBlank(courseTeacherDTO.getTeacherName()) ||
                StringUtils.isBlank(courseTeacherDTO.getIntroduction()) ||
                StringUtils.isBlank(courseTeacherDTO.getPosition()) ||
                ObjectUtils.isEmpty(companyId)) {
            ExceptionCast.cast(ContentErrorCode.E_120038);
        }
        //先根據課程id和機構id查詢
        LambdaQueryWrapper<CourseBase> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseBase::getCompanyId, companyId);
        queryWrapper.eq(CourseBase::getId, courseTeacherDTO.getCourseId());
        queryWrapper.eq(CourseBase::getStatus, CommonEnum.USING_FLAG.getCodeInt());
        CourseBase courseBaseOne = courseBase.getOne(queryWrapper);
        //判斷業務數據
        if (ObjectUtils.isEmpty(courseBaseOne)) {
            ExceptionCast.cast(ContentErrorCode.E_120013);
        }
        if (!(CourseAuditEnum.AUDIT_UNPAST_STATUS.getCode().equals(courseBaseOne.getAuditStatus())) &&
                !(CourseAuditEnum.AUDIT_DISPAST_STATUS.getCode().equals(courseBaseOne.getAuditStatus()))
        ) {
            ExceptionCast.cast(ContentErrorCode.E_1200504);
        }

        //根據有無教師id查詢是新增或是修改
        CourseTeacher courseTeacher = CourseTeacherConvert.INSTANCE.dto2Po(courseTeacherDTO);
        if (ObjectUtils.isEmpty(courseTeacherDTO.getCourseTeacherId())) {
            //新增操作
            boolean save = this.save(courseTeacher);
            if (!save) {
                ExceptionCast.cast(ContentErrorCode.E_120038);
            }
        } else {
            //修改操作
            boolean b = this.updateById(courseTeacher);
            if (!b) {
                ExceptionCast.cast(ContentErrorCode.E_120038);
            }
        }
        //id回顯查詢最新數據
        CourseTeacher teacher = this.getById(courseTeacher.getId());
        //封裝數據返回
        return CourseTeacherConvert.INSTANCE.po2dto(teacher);
    }

    /**
     * @param courseBaseId
     * @param courseTeacherId
     * @param companyId
     * @return void
     * @Description //刪除教師信息
     * @Date 2021/12/15 23:41
     * @Param [courseBaseId, courseTeacherId, companyId]
     * <p>
     * 開啓十五
     * 關鍵數據-->courseBaseId  courseTeacherId companyId
     * 業務數據-->是否有課程.課程是否被刪除.課程狀態支持部.課程與機構id比較.
     */
    @Override
    public void deleteTeacher(Long courseBaseId, Long courseTeacherId, Long companyId) {
        //判斷關鍵數據
        if (ObjectUtils.isEmpty(courseBaseId) ||
                ObjectUtils.isEmpty(courseTeacherId) ||
                ObjectUtils.isEmpty(companyId)) {
            ExceptionCast.cast(ContentErrorCode.E_120038);
        }
        //根據id和教育機構id查詢課程表
        LambdaQueryWrapper<CourseBase> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseBase::getId, courseBaseId);
        queryWrapper.eq(CourseBase::getCompanyId, companyId);
        queryWrapper.eq(CourseBase::getStatus, CommonEnum.USING_FLAG.getCodeInt());
        CourseBase courseBaseOne = courseBase.getOne(queryWrapper);
        //判斷業務數據
        if (ObjectUtils.isEmpty(courseBaseOne)) {
            ExceptionCast.cast(ContentErrorCode.E_120013);
        }
        if (!(CourseAuditEnum.AUDIT_UNPAST_STATUS.getCode().equals(courseBaseOne.getAuditStatus())) &&
                !(CourseAuditEnum.AUDIT_DISPAST_STATUS.getCode().equals(courseBaseOne.getAuditStatus()))
        ) {
            ExceptionCast.cast(ContentErrorCode.E_1200504);
        }
        LambdaQueryWrapper<CourseTeacher> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(CourseTeacher::getId, courseTeacherId);
        lambdaQueryWrapper.eq(CourseTeacher::getCourseId, courseBaseId);
        List<CourseTeacher> list = this.list(lambdaQueryWrapper);
        if (list.size() != 1) {
            ExceptionCast.cast(ContentErrorCode.E_120013);
        }
        //實行刪除-->直接刪除不用邏輯刪除
        boolean b = this.removeById(courseTeacherId);
        if (!b) {
            ExceptionCast.cast(ContentErrorCode.E_120035);
        }
    }
}
